package com.redlight.base;

import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import com.redlight.jdbc.Column;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by chao on 2019/1/9.
 */
public class SysUser extends BaseEntity {
    @Column("login_name")
    private String loginName;
    @Column("password")
    private String password;
    @Column("no")
    private String no;
    @Column("name")
    private String name;
    @Column("email")
    private String email;
    @Column("phone")
    private String phone;
    @Column("mobile")
    private String mobile;
    @Column("sex")
    private String sex;
    @Column("cardId")
    private String cardId;
    @Column("birthday")
    private String birthday;
    @Column("entryDate")
    private String entryDate;
    @Column("address")
    private String address;
    @Column("photo")
    private String photo;
    @Column("job")
    private String job;
    private String jobLabel;
    @Column("qrCode")
    private String qrCode;
    @Column("login_flag")
    private String loginFlag;
    @Column("allow_mobile")
    private String allowMobile;
    private String field1;
    private String field2;
    private String field3;
    private String loginIp;
    private Date loginDate;
    private String validCode;
    private Date validCodeDate;
    private List deptList = new ArrayList();
    private String companyId;
    private String deptId;
    private String nameParam;
    private String loginNameParam;
    private List<String> roleIdList = Lists.newArrayList();
    private List<SysRoleBase> roleList = Lists.newArrayList();

    public SysUser() {
    }

    public SysUser(String id) {
        super(id);
    }

    public String getPhoto() {
        return this.photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public String getId() {
        return this.id;
    }

    public String getLoginName() {
        return this.loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return this.name;
    }

    public String getNo() {
        return this.no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMobile() {
        return this.mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getLoginIp() {
        return this.loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    public Date getLoginDate() {
        return this.loginDate;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    public boolean isAdmin() {
        if(this.roleList != null && 0 < this.roleList.size()) {
            Iterator var1 = this.roleList.iterator();

            while(var1.hasNext()) {
                SysRoleBase role = (SysRoleBase)var1.next();
                if("security-role".equalsIgnoreCase(role.getRoleType())) {
                    return true;
                }
            }
        }

        return this.id != null && "1".equals(this.id);
    }

    public List<SysRoleBase> getRoleList() {
        return this.roleList;
    }

    public void setRoleList(List<SysRoleBase> roleList) {
        this.roleList = roleList;
    }

    public String getLoginFlag() {
        return this.loginFlag;
    }

    public void setLoginFlag(String loginFlag) {
        this.loginFlag = loginFlag;
    }

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getCardId() {
        return this.cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getBirthday() {
        if("".equals(this.birthday)) {
            this.birthday = null;
        }

        return this.birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getEntryDate() {
        if("".equals(this.entryDate)) {
            this.entryDate = null;
        }

        return this.entryDate;
    }

    public void setEntryDate(String entryDate) {
        this.entryDate = entryDate;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getQrCode() {
        return this.qrCode;
    }

    public void setQrCode(String qrCode) {
        this.qrCode = qrCode;
    }

    public String getValidCode() {
        return this.validCode;
    }

    public void setValidCode(String validCode) {
        this.validCode = validCode;
    }

    public Date getValidCodeDate() {
        return this.validCodeDate;
    }

    public void setValidCodeDate(Date validCodeDate) {
        this.validCodeDate = validCodeDate;
    }

    public String getAllowMobile() {
        return this.allowMobile;
    }

    public void setAllowMobile(String allowMobile) {
        this.allowMobile = allowMobile;
    }

    public String getField1() {
        return this.field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField2() {
        return this.field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public String getField3() {
        return this.field3;
    }

    public void setField3(String field3) {
        this.field3 = field3;
    }

    public List getDeptList() {
        return this.deptList;
    }

    public void setDeptList(List deptList) {
        this.deptList = deptList;
    }

    public String getJob() {
        return this.job;
    }

    public void setJob(String job) {
        this.job = job;
    }

//    public String getJobLabel() {
//        if(StringUtil.isNotBlank(this.getJob())) {
//            this.jobLabel = DictUtils.getDictLabel(this.getJob(), "jobType", "");
//        }
//
//        return this.jobLabel;
//    }

    public void setJobLabel(String jobLabel) {
        this.jobLabel = jobLabel;
    }

    public String getCompanyId() {
        return this.companyId;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    @JSONField(
            serialize = false
    )
    public String getDeptId() {
        return this.deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    @JSONField(
            serialize = false
    )
    public String getNameParam() {
        return this.nameParam;
    }

    public void setNameParam(String nameParam) {
        this.nameParam = nameParam;
    }

    @JSONField(
            serialize = false
    )
    public String getLoginNameParam() {
        return this.loginNameParam;
    }

    public void setLoginNameParam(String loginNameParam) {
        this.loginNameParam = loginNameParam;
    }

    public String getRoleNames() {
        return CollectionUtil.extractToString(this.roleList, "name", ",");
    }

    @JSONField(
            serialize = false
    )
    public String[] getRoleIds() {
        List<String> roleIdList = new ArrayList();
        Iterator var2 = this.roleList.iterator();

        while(var2.hasNext()) {
            SysRoleBase role = (SysRoleBase)var2.next();
            roleIdList.add(role.getId());
        }

        return (String[])roleIdList.toArray(new String[roleIdList.size()]);
    }

    public void setRoleIds(String roleIds) {
        if(null != roleIds) {
            this.roleList = Lists.newArrayList();
            String[] roleIdArr = roleIds.split(",");
            String[] var3 = roleIdArr;
            int var4 = roleIdArr.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String roleId = var3[var5];
                SysRoleBase role = new SysRoleBase();
                role.setId(roleId);
                this.roleList.add(role);
            }
        }

    }

    @JSONField(
            serialize = false
    )
    public String dataScopeFilter(String deptAlias, String userAlias) {
        StringBuilder sqlString = new StringBuilder();
        List<String> dataScope = Lists.newArrayList();
        if(!this.isAdmin() && StringUtils.isNotBlank(this.getId())) {
            boolean isDataScopeAll = false;
            Iterator var6 = this.getRoleList().iterator();

            while(var6.hasNext()) {
                SysRoleBase r = (SysRoleBase)var6.next();
                String[] var8 = StringUtils.split(deptAlias, ",");
                int var9 = var8.length;

                for(int var10 = 0; var10 < var9; ++var10) {
                    String oa = var8[var10];
                    if(!dataScope.contains(r.getDataScope()) && StringUtils.isNotBlank(oa)) {
                        if("1".equals(r.getDataScope())) {
                            isDataScopeAll = true;
                        } else if(!"2".equals(r.getDataScope()) && !"3".equals(r.getDataScope()) && !"4".equals(r.getDataScope()) && !"5".equals(r.getDataScope()) && "9".equals(r.getDataScope())) {
                            String deptIds = StringUtils.join(r.getOfficeIdList(), "','");
                            if(StringUtils.isNotEmpty(deptIds)) {
                                sqlString.append(" OR " + oa + ".id IN ('" + deptIds + "')");
                            }
                        }

                        dataScope.add(r.getDataScope());
                    }
                }
            }

            if(!isDataScopeAll) {
                String[] var13;
                int var14;
                int var15;
                String ua;
                if(StringUtils.isNotBlank(userAlias)) {
                    var13 = StringUtils.split(userAlias, ",");
                    var14 = var13.length;

                    for(var15 = 0; var15 < var14; ++var15) {
                        ua = var13[var15];
                        sqlString.append(" OR " + ua + ".id = '" + this.getId() + "'");
                    }
                } else {
                    var13 = StringUtils.split(deptAlias, ",");
                    var14 = var13.length;

                    for(var15 = 0; var15 < var14; ++var15) {
                        ua = var13[var15];
                        sqlString.append(" OR " + ua + ".id IS NULL");
                    }
                }
            } else {
                sqlString = new StringBuilder();
            }
        }

        return StringUtils.isNotBlank(sqlString.toString())?" AND (" + sqlString.substring(4) + ")":"";
    }

    public List<String> getRoleIdList() {
        return this.roleIdList;
    }

    public void setRoleIdList(List<String> roleIdList) {
        this.roleIdList = roleIdList;
    }

    public int hashCode() {
        return this.id.hashCode();
    }

    public boolean equals(Object obj) {
        if(!(obj instanceof SysUser)) {
            return false;
        } else {
            SysUser p = (SysUser)obj;
            return this.id.equals(p.id);
        }
    }

    @Override
    public String toString() {
        return "SysUser{" +
                "loginName='" + loginName + '\'' +
                ", password='" + password + '\'' +
                ", no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", mobile='" + mobile + '\'' +
                ", sex='" + sex + '\'' +
                ", cardId='" + cardId + '\'' +
                ", birthday='" + birthday + '\'' +
                ", entryDate='" + entryDate + '\'' +
                ", address='" + address + '\'' +
                ", photo='" + photo + '\'' +
                ", job='" + job + '\'' +
                ", jobLabel='" + jobLabel + '\'' +
                ", qrCode='" + qrCode + '\'' +
                ", loginFlag='" + loginFlag + '\'' +
                ", allowMobile='" + allowMobile + '\'' +
                ", field1='" + field1 + '\'' +
                ", field2='" + field2 + '\'' +
                ", field3='" + field3 + '\'' +
                ", loginIp='" + loginIp + '\'' +
                ", loginDate=" + loginDate +
                ", validCode='" + validCode + '\'' +
                ", validCodeDate=" + validCodeDate +
                ", deptList=" + deptList +
                ", companyId='" + companyId + '\'' +
                ", deptId='" + deptId + '\'' +
                ", nameParam='" + nameParam + '\'' +
                ", loginNameParam='" + loginNameParam + '\'' +
                ", roleIdList=" + roleIdList +
                ", roleList=" + roleList +
                '}';
    }
}
